using System;
using System.Collections.Generic;
using System.Text;
using Xeml.Document;

namespace Xeml.Sampling
{
    public class VariableColumn : Column
    {

        private DynamicTerm _term;
        private string _context;
        private ParameterColumnLevel _columnLevel;

        public ParameterColumnLevel ColumnLevel
        {
            get { return _columnLevel; }
            set
            {
                _columnLevel = value;
                SetNames();
            }
        }


        public string Context
        {
            get { return _context; }
            set { _context = value; SetNames(); }
        }


        public DynamicTerm Term
        {
            get { return _term; }
            set
            {
                _term = value;
                SetNames();
            }
        }

        private void SetNames()
        {

            if (_term != null)
            {
                switch (_columnLevel)
                {
                    case ParameterColumnLevel.Parameter:
                        {
                            base.name = _term.TermId;
                            StringBuilder sb = new StringBuilder();

                            sb.AppendFormat("NsAlias: {0}\r\n", _term.NameSpaceAlias);
                            sb.AppendFormat("Name: {0}\r\n", _term.Name);
                            base.DisplayName = sb.ToString();
                            //if (_parameter.NameSpace != null)
                            //    base.NodeDisplayName = _parameter.FqName.Replace(_parameter.NameSpace, _parameter.NameSpaceAlias);
                            //else
                            //    base.NodeDisplayName = _parameter.NameSpaceAlias + "." + _parameter.FqName;
                            base.NodeDisplayName = _term.Name;

                            break;
                        }
                    case ParameterColumnLevel.Context:
                        {
                            base.name = _term.Name + ":" + _context;
                            StringBuilder sb = new StringBuilder();
                            sb.AppendFormat("NsAlias: {0}\r\n", _term.NameSpaceAlias);
                            //sb.AppendFormat("Group: {0}\r\n", _parameter.GroupPath);
                            sb.AppendFormat("Context: {0}", _context);
                            base.DisplayName = sb.ToString();
                            base.NodeDisplayName = _context;
                            break;
                        }
                    case ParameterColumnLevel.Time:
                        {

                            DynamicTerm dterm = _term as DynamicTerm;
                            base.name = _term.Name + ":" + _context + "@" + time.ToString();
                            //if (_parameter.NameSpace != null)
                            //    base.DisplayName = _parameter.FqName.Replace(_parameter.NameSpace, _parameter.NameSpaceAlias) + ":\r\n" + _context + "@" + time.ToString();

                            StringBuilder sb = new StringBuilder();
                            //sb.AppendFormat("NsAlias: {0}\r\n", _parameter.NameSpaceAlias);
                            //sb.AppendFormat("Group: {0}\r\n", _parameter.GroupPath);

                            if (!string.IsNullOrEmpty(_context) && !string.IsNullOrEmpty(dterm.ValueBy(_context)[0].Unit))
                                sb.AppendFormat("Name: {0}({1})\r\n", _term.Name, dterm.ValueBy(_context)[0].Unit);
                            else
                                sb.AppendFormat("Name: {0}\r\n", _term.Name);


                            sb.AppendFormat("Context: {0}\r\n", _context);
                            sb.AppendFormat("Time: {0}", time.ToString());


                            base.DisplayName = sb.ToString();
                            base.NodeDisplayName = time.ToString();

                            break;
                        }
                    default:
                        break;
                }

            }
        }



        private TimeSpan time;

        public TimeSpan Time
        {
            get { return time; }
            set { time = value; SetNames(); }
        }


        public VariableColumn()
        {
        }

        public override object ExtractRowValue(object extract)
        {

            if (!(extract is StoryBase))
                throw new ArgumentException("Argument type is not StoryBase");

            StoryBase sb = extract as StoryBase;

            SortedDictionary<TimeSpan, ValueBase> vals = sb.SortedHistoryValues(this._term.TermId, this.Context);
            if (vals.ContainsKey(this.Time))
            {
                ValueBase vb = vals[this.Time];
                if (vb is DynamicValue)
                    return (vb as DynamicValue).Val;
                else if (vb is Cycle)
                    return (string)(vb as Cycle);
                else
                    return "-";
            }
            else
            {
                if (vals.Count > 0)
                {
                    ValueBase vb = null;
                    foreach (KeyValuePair<TimeSpan, ValueBase> kvp in vals)
                    {
                        if (kvp.Key <= this.time)
                            vb = kvp.Value;
                        else
                            break;
                    }
                    if (vb is DynamicValue)
                        return (vb as DynamicValue).Val;
                    else if (vb is Cycle)
                        return (string)(vb as Cycle);
                    else
                        return "-";
                }
                return "-";
            }
        }
    }

    public enum ParameterColumnLevel
    {
        Parameter,
        Context,
        Time
    }
}
